$(function () {
    var data = null;
    $.ajax({
        type: "get",
        url: "/address/queryAddress",
        success: function (res) {
            console.log(res);
            data = res
            $('.addInfo').html(template('addTemp', res))
        }
    });
    //删除地址
    $('.addInfo').on('tap', '.delBtn', function () {
        var li = $(this).parents('.swipeLi')[0];
        console.log(this);
        var id = $(this).data('id')
        mui.confirm('是否删除地址', function (message) {
            if (message.index == 1) {
                $.ajax({
                    type: "post",
                    url: "/address/deleteAddress",
                    data: {
                        id
                    },
                    success: function (res) {
                        if (res.success) {
                            location.reload();
                        }
                    }
                });
            } else {
                //关闭侧滑（要关闭的元素）dom元素
                mui.swipeoutClose(li);
            }
        })
    })
    //编辑
    $('.addInfo').on('tap', '.editBtn', function () {
        var id = $(this).data('id')
        for (var i = 0; i < data.length; i++) {
            if (data[i].id == id) {
                localStorage.setItem('editAddress', JSON.stringify(data[i]));
                break;
            }
        }
        location.href = 'addAddress.html?isEdit=1'
    })
})